home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Freeware / Griffith 0.9.8 / griffith-0.9.8-win32.exe / {app} / lib / gemail.py < prev    next >
Text File  |  2008-11-17  |  3KB  |  86 lines

  1. # -*- coding: UTF-8 -*-
  2.  
  3. __revision__ = '$Id: gemail.py 1040 2008-11-15 21:13:49Z mikej06 $'
  4.  
  5. # Copyright (c) 2005-2007 Vasco Nunes
  6.  
  7. # This program is free software; you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation; either version 2 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. # GNU Library General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with this program; if not, write to the Free Software
  19. # 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
  20.  
  21. # You may use and distribute this software under the terms of the
  22. # GNU General Public License, version 2 or later
  23.  
  24. import socket
  25. import smtplib
  26. import gutils
  27.  
  28. socket.setdefaulttimeout(10)
  29.  
  30. def mailto(self, tls, port, server, auth, user, password, sender, to, subject, msg):
  31.     """
  32.     sends an email
  33.     """
  34.     try:
  35.         session = smtplib.SMTP(server, port)
  36.         session.set_debuglevel(1)
  37.     except socket.timeout:
  38.         gutils.error(self, _("Connection timed out"), \
  39.             self.widgets['window'])
  40.         return()
  41.     if tls == True:
  42.         session.ehlo()
  43.         session.starttls()
  44.         session.ehlo()
  45.     if auth:
  46.         try:
  47.             session.login(user, password)
  48.         except:
  49.             gutils.error(self, _("Error sending e-mail: %s")%_("login failure"), \
  50.                 self.widgets['window'])
  51.             return
  52.     headers = "From: %s\r\nTo: %s\r\nSubject: %s\r\n\r\n" \
  53.         % (sender, to, subject)
  54.     try:
  55.         smtpresult = session.sendmail(sender, to, headers+msg)
  56.         gutils.info(self, _("E-mail sent sucessfuly"), self.widgets['window'])
  57.         return
  58.     except:
  59.         gutils.error(self, _("Error sending e-mail: %s")%"", self.widgets['window'])
  60.         
  61.     session.quit()
  62.  
  63. def send_email(self):
  64.     if len(self.person_email):
  65.         if self.config.get('use_auth', False, section='mail') == True:
  66.             use_auth = 1
  67.         else:
  68.             use_auth = 0
  69.         try:
  70.             mailto(self, self.config.get('mail_use_tls', False, section='mail'), \
  71.                 self.config.get('mail_smtp_port', '25', section='mail'), \
  72.                 self.config.get('smtp_server', 'localhost', section='mail'), \
  73.                 use_auth, self.config.get('username', '', section='mail'), \
  74.                 self.config.get('password', '', section='mail'), \
  75.                 self.config.get('email', 'griffith', section='mail'), self.person_email, \
  76.                 _("Movie loan reminder"), _("Hi, %s!\n\nJust to remind you " + \
  77.                 "that I'm really needing the following movie I have loaned to you " + \
  78.                 "recently:\n\n%s (%s)\n\nLoaned on %s") \
  79.                 %(self.person_name, self.widgets['movie']['o_title'].get_text().decode('utf-8'), \
  80.                 self.widgets['movie']['title'].get_text().decode('utf-8'), self.loan_date[:10]))
  81.         except:
  82.             gutils.error(self, _("Mail could not be sent. Please check e-mail preferences."), self.widgets['window'])
  83.     else:
  84.         gutils.info(self, _("This person has no e-mail address defined."), \
  85.             self.widgets['window'])
  86.